package net.edu_soft.ec.asset.controller;


import com.alibaba.fastjson.JSONObject;
import net.edu_soft.ec.asset.pojo.Label;
import net.edu_soft.ec.asset.service.LabelService;
import net.edu_soft.ec.common.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *  @author: PUING
 *  @Date: 2020/4/7 11:27
 *  @Description: 标签控制类
 */
@RestController
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    /***
     * 根据id查询单条标签
     * @param id 标签id
     * @return
     */
    @RequestMapping(value = "/findById")
    public ResultVo findById(Integer id){
        return ResultVo.succeed("获取成功",labelService.findById(id));
    }

    /***
     * 分页查询标签,可以根据名称模糊查询
     * @param name 名称
     * @param pageNum 当前页
     * @param pageSize 每页数
     * @return
     */
    @RequestMapping(value = "/findList")
    public ResultVo findList(String name,Integer pageNum,Integer pageSize) {
        return ResultVo.succeed("标签列表",labelService.findList(name,pageNum,pageSize));
    }

    /***
     * 添加单个标签
     * @param labelName 标签
     * @return
     */
    @RequestMapping(value = "/save")
    public ResultVo save(String  labelName){
        String m = 1==labelService.save(labelName)?"成功添加":"添加失败";
        return ResultVo.succeed(m,null);
    }

    /***
     * 批量添加标签
     * @param labels 标签名，数组形式
     * @return
     */
    @RequestMapping(value = "/saveList")
    public ResultVo saveList(@RequestBody String labels){
//        System.out.println("数据： "+labels);
//        List<String> labelList = JSONObject.parseArray(labels, String.class);
//        System.out.println("集合： "+labelList.toString());

        String m = 1==labelService.saveList(labels)?"成功添加":"添加失败";
        return ResultVo.succeed(m,null);
    }

    /***
     * 修改标签
     * @param label 标签
     * @return
     */
    @RequestMapping(value = "/update")
    public ResultVo update(Label label){
        String m = 1==labelService.update(label)?"成功修改":"修改失败";
        return ResultVo.succeed(m,null);
    }

    /***
     * 批量修改标签
     * @param labels 标签集合
     * @return
     */
    @RequestMapping(value = "/updateList")
    public ResultVo updateList(@RequestBody String labels){
        System.out.println("数据： "+labels);
//        List<Label> labelList = (List<Label>) JSONObject.parseArray(labels, Label.class);
//        System.out.println("集合： "+labelList.toString());

        String m = 1==labelService.updateList(labels)?"成功修改":"修改失败";
        return ResultVo.succeed(m,null);
       // return ResultVo.succeed("批量修改标签",labels);
    }

    /***
     * 批量删除标签
     * @param ids  id串
     * @param isDel 是否删除
     * @return
     */
    @RequestMapping(value = "/delList")
    public ResultVo delList(String ids,Integer isDel){
        String m = 1==labelService.delList(ids,isDel)?"成功删除":"删除失败";
        return ResultVo.succeed(m,null);

  }

}
